package Exam.ArrayListDemo.StudentSystem;

import java.util.ArrayList;
import java.util.Scanner;

public class StudentSystem {
    public static void startStudentSystem() {
        ArrayList<Student> list = new ArrayList<>();
        loop:
        while (true) {
            System.out.println("---------------欢迎来到学生管理系统---------------");
            System.out.println("1.添加学生");
            System.out.println("2.删除学生");
            System.out.println("3.修改学生");
            System.out.println("4.查询学生");
            System.out.println("5.退出");
            System.out.println("请输入您的选择");

            Scanner sc = new Scanner(System.in);
            String choose = sc.next();

            switch (choose) {
                case "1" -> addStudent(list);
                case "2" -> deleteStudent(list);
                case "3" -> updateStudent(list);
                case "4" -> queryStudent(list);
                case "5" -> {
                    System.out.println("退出");
                    //跳出大循环
                    //法一：
                    break loop;
                    //法二：
                    //System.exit(0);//停止虚拟机运行
                }
                default -> System.out.println("没有这个选项");
            }
        }
    }

    //添加学生
    public static void addStudent(ArrayList<Student> list) {
        Student s = new Student();
        Scanner sc = new Scanner(System.in);
        String id = null;
        while (true) {
            System.out.println("请输入学生id");
            id = sc.next();
            boolean flag = contains(list, id);
            if (flag) {
                //id存在，不能添加
                System.out.println("id已存在");
                return;
            } else {
                //id不存在,可以添加
                s.setId(id);
                break;
            }
        }

        System.out.println("请输入学生姓名");
        String name = sc.next();
        s.setName(name);
        System.out.println("请输入学生年龄");
        int age = sc.nextInt();
        s.setAge(age);
        System.out.println("请输入学生地址");
        String address = sc.next();
        s.setAddress(address);

        list.add(s);
        System.out.println("添加成功");

    }

    //删除学生
    public static void deleteStudent(ArrayList<Student> list) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入要删除的id");
        String id = sc.next();
        //查询id在集合中的索引
        int index = getIndex(list, id);
        //对index进行判断
        //如果大于等于e的，表示存在，直接删除
        //如果-1，就表示不存在，结束方法，回到初始菜单
        if (index >= 0) {
            list.remove(index);
            System.out.println("id为" + id + "删除成功");
        } else {
            System.out.println("没有此id");
        }

    }

    //修改学生
    public static void updateStudent(ArrayList<Student> list) {
        /*
        * 键盘录入要修改的学生id，需要满足以下要求
        ·id存在，继续录入其他信息
        ·id不存在，需要提示不存在，并回到初始菜单
        * */
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入要修改的id");
        String id = sc.next();
        int index = getIndex(list,id);
        //判断index
        if (index == -1) {
            System.out.println("要修改的id" + id + "不存在");
            return;
        }

        Student stu = list.get(index);

        System.out.println("请输入学生姓名");
        String newname = sc.next();
        stu.setName(newname);
        System.out.println("请输入学生年龄");
        int age = sc.nextInt();
        stu.setAge(age);
        System.out.println("请输入学生地址");
        String address = sc.next();
        stu.setAddress(address);

        System.out.println("修改成功");

    }

    //查询学生
    public static void queryStudent(ArrayList<Student> list) {
        //判断集合是否为空
        if (list.size() == 0) {
            System.out.println("没有学生信息");
            return;
        }
        //打印表头信息
        System.out.println("编号\t\t姓名\t\t年龄\t\t地址");
        for (int i = 0; i < list.size(); i++) {
            Student stu = list.get(i);
            System.out.println(stu.getId() + "\t\t" + stu.getName() + "\t\t" + stu.getAge() + "\t\t" + stu.getAddress());
        }


    }

    //判断id在集合中是否存在
    public static boolean contains(ArrayList<Student> list, String id) {
//        for (int i = 0; i < list.size(); i++){
//            Student stu = list.get(i);
//            if (stu.getId().equals(id)){
//                return true;
//            }
//        }
//        return false;
        return getIndex(list, id) >= 0;
    }

    //通过id获取索引
    public static int getIndex(ArrayList<Student> list, String id) {
        for (int i = 0; i < list.size(); i++) {
            Student stu = list.get(i);
            if (stu.getId().equals(id)) {
                return i;
            }
        }
        return -1;
    }


}
